//
//  DFNewFeatureViewController.swift
//  FlyBlog
//
//  Created by FEI on 16/4/12.
//  Copyright © 2016年 FEI. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class DFNewFeatureViewController: UICollectionViewController {

    let layout = UICollectionViewFlowLayout()

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    init() {
        super.init(collectionViewLayout: layout)
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false

    }

    override func viewDidLoad() {
        super.viewDidLoad()

        self.collectionView!.registerClass(DFNewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)

    }



    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return 4
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! DFNewFeatureCell
        cell.index = indexPath.item
        return cell
    }
}

extension DFNewFeatureViewController {
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        let index = Int(scrollView.contentOffset.x / UIScreen.mainScreen().bounds.width)

        if index == 3 {
            let indexPath = NSIndexPath(forItem: index, inSection: 0)
            let cell = collectionView?.cellForItemAtIndexPath(indexPath) as! DFNewFeatureCell
            cell.showBeginButton()
        }
    }
}








class DFNewFeatureCell: UICollectionViewCell {
        var index: Int = 0 {
            didSet{
                let imageName = "new_feature_\(index + 1)"
                bkgroundView.image = UIImage(named: imageName)
                beginButton.hidden = true
            }
        }

    override init(frame: CGRect) {
        super.init(frame: frame)

        prepareUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


    private func prepareUI () {

        contentView.addSubview(bkgroundView)
        contentView.addSubview(beginButton)
        bkgroundView.snp_makeConstraints {
            (make) -> Void in
            make.edges.equalTo(UIEdgeInsetsZero)
        }
        beginButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(contentView)
            make.bottom.equalTo(contentView).offset(-160)
        }

    }
    func showBeginButton() {
        beginButton.hidden = false
        beginButton.transform = CGAffineTransformMakeScale(0, 0)

        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.beginButton.transform = CGAffineTransformIdentity
            }) { (_) -> Void in

                }
        }

    @objc private func didClickBeginButton() {
        let controller = DFMainViewController()
        (UIApplication.sharedApplication().delegate as! AppDelegate).switchViewController(controller)
    }




    //MARK:懒加载
    private lazy var bkgroundView: UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))

    private lazy var beginButton : UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        btn.setTitle("开始体验", forState: UIControlState.Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(18)
        btn.addTarget(self, action: Selector("didClickBeginButton"), forControlEvents: UIControlEvents.TouchUpInside)
        btn.sizeToFit()
        return btn
    }()
}
